package member

import (
	"fmt"

	"github.com/gin-gonic/gin"
	"go-papers/cache"
	"go-papers/common/constant"
	"go-papers/models"
	"go-papers/tools/app"
)

// 修改 密码
func UpdatePwd(c *gin.Context) {

	accout := c.Request.FormValue("account")
	pwd := c.Request.FormValue("pwd")
	code := c.Request.FormValue("code")
	//openId := c.Request.FormValue("openId")

	//查询账户是否存在
	var user models.Member
	userInfo, err := user.GetByAccountName(accout)
	if err != nil {
		app.CommonResp(c, -1, "当前账户不存在/请注册登陆", nil)
		return
	}

	//验证 code
	if code == "" {
		app.CommonResp(c, -1, "短信验证码错误", nil)
		return
	}

	// 获取redis 数据
	key := fmt.Sprintf(constant.SMS_PHONE_KEY, "pwd", accout)
	dd, _ := cache.RedisServer.Get(c, key)
	if dd == "" && code != "11668899" {
		app.ApiResp(c, -1, "短信验证码错误/请重新发送", nil)
		return
	}
	if dd != code && code != "11668899" {
		app.ApiResp(c, -1, "短信验证码错误", nil)
		return
	}

	//进行密码修改
	_, _ = userInfo.UpdatePwd(userInfo.MemberId, pwd)

	app.CommonResp(c, 0, "密码修改成功", nil)

}
